using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Naptime.Extensions;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Distribution;
using Voodoo.GameOps;
using Voodoo.Pattern;

public class Shop : SingletonMB<Shop>
{
	public AudioSource upgradeSource;
	[SerializeField]
	private Button longButton;

	[SerializeField]
	private Button oilButton;

	[SerializeField]
	private Button powerButton;

	[SerializeField]
	private Button sizeButton;

	[SerializeField]
	private TextMeshProUGUI _longCost;

	[SerializeField]
	private TextMeshProUGUI _oilCost;

	[SerializeField]
	private TextMeshProUGUI _powerCost;

	[SerializeField]
	private TextMeshProUGUI _sizeCost;

	[SerializeField]
	private Button RvlongButton;

	[SerializeField]
	private Button RvoilButton;

	[SerializeField]
	private Button RvpowerButton;

	[SerializeField]
	private Button RvsizeButton;

	[SerializeField]
	private Text _RvlongCost;

	[SerializeField]
	private Text _RvoilCost;

	[SerializeField]
	private Text _RvpowerCost;

	[SerializeField]
	private Text _RvsizeCost;

	[SerializeField]
	private Button ForceRvPowerButton;

	[SerializeField]
	private Button ForceRvSizeButton;

	[SerializeField]
	private bool[] isForcedRvPower;

	[SerializeField]
	private bool[] isForcedRvSize;

	[SerializeField]
	private GameObject IncrementalLong;

	[SerializeField]
	private GameObject IncrementalOil;

	[SerializeField]
	private GameObject IncrementalPower;

	[SerializeField]
	private GameObject IncrementalSize;

	[SerializeField]
	private GameObject IncrementalLongOnboardingArea;

	[SerializeField]
	private GameObject IncrementalOilOnboardingArea;

	[SerializeField]
	private GameObject IncrementalPowerOnboardingArea;

	[SerializeField]
	private GameObject IncrementalSizeOnboardingArea;

	[SerializeField]
	private Text OilBonus;

	[SerializeField]
	private Text PowerBonus;

	[SerializeField]
	private Text SizeBonus;

	[SerializeField]
	private Color white;

	[SerializeField]
	private Color red;

	[SerializeField]
	private Item discountItem;

	private Money _moneyClass;

	private Upgrade _upgrade;

	public int[] longCosts;

	public int[] oilCosts;

	public int[] powerCosts;

	public int[] sizeCosts;

	public int[] originalOilCosts;

	private int _currentLong;

	private int _currentOil;

	private int _currentPower;

	private int _currentSize;

	private int _currentLongCost;

	private int _currentOilCost;

	private int _currentPowerCost;

	private int _currentSizeCost;

	public Item OilMaxUpgrade;

	public int MaxLengthLevel => longCosts.Length;

	public int MaxOilLevel => originalOilCosts.Length;

	public int MaxPowerLevel => powerCosts.Length;

	public int MaxSizeLevel => sizeCosts.Length;

	public int CurrentLengthLevel => _currentLong;

	public int CurrentOilLevel => _currentOil;

	public int CurrentPowerLevel => _currentPower;

	public int CurrentSizeLevel => _currentSize;

	public static event Action OnChecked;

	private void Start()
	{
		if (Preloader.ShopConfig.LengthUpgrades != null && Preloader.ShopConfig.LengthUpgrades.Length != 0)
		{
			longCosts = Preloader.ShopConfig.LengthUpgrades;
		}
		if (Preloader.ShopConfig.FuelUpgrades != null && Preloader.ShopConfig.FuelUpgrades.Length != 0)
		{
			oilCosts = Preloader.ShopConfig.FuelUpgrades;
		}
		if (Preloader.ShopConfig.PowerUpgrades != null && Preloader.ShopConfig.PowerUpgrades.Length != 0)
		{
			powerCosts = Preloader.ShopConfig.PowerUpgrades;
		}
		if (Preloader.ShopConfig.SizeUpgrades != null && Preloader.ShopConfig.SizeUpgrades.Length != 0)
		{
			sizeCosts = Preloader.ShopConfig.SizeUpgrades;
		}
		_moneyClass = UnityEngine.Object.FindObjectOfType<Money>(includeInactive: true);
		_upgrade = UnityEngine.Object.FindObjectOfType<Upgrade>();
		_moneyClass.UpdateValue();
		CheckButtons();
		StartCoroutine(CheckButtonRoutine());
	}

	private IEnumerator CheckButtonRoutine()
	{
		yield return new WaitForEndOfFrame();
		yield return new WaitForEndOfFrame();
		CheckButtons();
	}

	protected override void OnDestroySpecific()
	{
		if (SingletonMB<Shop>.Instance == this)
		{
			Shop.OnChecked.ClearDelegates();
		}
	}

	public void CheckButtons()
	{
		if (SingletonMB<DuelModeManager>.Instance != null)
		{
			return;
		}
		OnboardingManager.IncrementalButtonStates incrementalStates = SingletonMB<OnboardingManager>.Instance.GetIncrementalStates();
		OilBonus.text = "+" + NintendoPlayerPrefs.GetInt("ScinOil", 0) + "%";
		PowerBonus.text = "+" + NintendoPlayerPrefs.GetInt("ScinPower", 0) + "%";
		SizeBonus.text = "+" + NintendoPlayerPrefs.GetInt("ScinSize", 0) + "%";
		OilBonus.gameObject.SetActive(NintendoPlayerPrefs.GetInt("ScinOil", 0) != 0);
		PowerBonus.gameObject.SetActive(NintendoPlayerPrefs.GetInt("ScinPower", 0) != 0);
		SizeBonus.gameObject.SetActive(NintendoPlayerPrefs.GetInt("ScinSize", 0) != 0);
		if (Preloader.OnboardingConfig.IsEnabled)
		{
			IncrementalLong.SetActive(incrementalStates.LongVisible);
			IncrementalOil.SetActive(incrementalStates.OilVisible);
			IncrementalPower.SetActive(incrementalStates.PowerVisible);
			IncrementalSize.SetActive(incrementalStates.SizeVisible);
		}
		_moneyClass?.UpdateValue();
		bool flag = true;
		_currentLong = NintendoPlayerPrefs.GetInt("Long", 0);
		_currentOil = NintendoPlayerPrefs.GetInt("Oil", 0);
		_currentPower = NintendoPlayerPrefs.GetInt("Power", 0);
		_currentSize = NintendoPlayerPrefs.GetInt("Size", 0);
		if (originalOilCosts == null || originalOilCosts.Length == 0)
		{
			originalOilCosts = oilCosts;
		}
		oilCosts = originalOilCosts;
		if (Preloader.ShopConfig.UpgradeShop)
		{
			List<int> list = oilCosts.ToList();
			for (int i = 0; i < InventoryManager.Instance.GetInventoryCount(OilMaxUpgrade); i++)
			{
				list.Add(list[list.Count - 1] + (list[list.Count - 1] - list[list.Count - 2]));
			}
			oilCosts = list.ToArray();
		}
		CalculatePrices();
		bool flag2 = false;
		bool flag3 = false;
		bool flag4 = false;
		bool flag5 = false;
		if (Preloader.OnboardingConfig.IsEnabled)
		{
			if (incrementalStates.LongIsFree && _currentLong == 0)
			{
				flag2 = true;
				_currentLongCost = 0;
			}
			if (incrementalStates.OilIsFree && _currentOil == 0)
			{
				flag3 = true;
				_currentOilCost = 0;
			}
			if (incrementalStates.PowerIsFree && _currentPower == 0)
			{
				flag4 = true;
				_currentPowerCost = 0;
			}
			if (incrementalStates.SizeIsFree && _currentSize == 0)
			{
				flag5 = true;
				_currentSizeCost = 0;
			}
		}
		SetIncrementalCost(_currentLongCost, RvlongButton.gameObject, longButton, _RvlongCost, _longCost, flag2, IncrementalLongOnboardingArea);
		SetIncrementalCost(_currentOilCost, RvoilButton.gameObject, oilButton, _RvoilCost, _oilCost, flag3, IncrementalOilOnboardingArea);
		SetIncrementalCost(_currentPowerCost, RvpowerButton.gameObject, powerButton, _RvpowerCost, _powerCost, flag4, IncrementalPowerOnboardingArea);
		SetIncrementalCost(_currentSizeCost, RvsizeButton.gameObject, sizeButton, _RvsizeCost, _sizeCost, flag5, IncrementalSizeOnboardingArea);
		if (Preloader.OnboardingConfig.IsEnabled)
		{
			bool flag6 = flag2 || flag3 || flag4 || flag5;
			SingletonMB<GameManager>.Instance.PlayButton.SetActive(!flag6);
		}
		if (Preloader.ABTestConfig.UpgradeRV && flag)
		{
			RvsizeButton.gameObject.SetActive(value: false);
			RvpowerButton.gameObject.SetActive(value: false);
		}
		Shop.OnChecked?.Invoke();
		void CalculatePrices()
		{
			int inventoryCount = InventoryManager.Instance.GetInventoryCount(discountItem);
			int @int = NintendoPlayerPrefs.GetInt("Level", 0);
			_currentLongCost = ((_currentLong >= longCosts.Length) ? (-1) : longCosts[_currentLong]);
			_currentOilCost = ((_currentOil >= oilCosts.Length) ? (-1) : oilCosts[_currentOil]);
			_currentPowerCost = ((_currentPower >= powerCosts.Length) ? (-1) : powerCosts[_currentPower]);
			_currentSizeCost = ((_currentSize >= sizeCosts.Length) ? (-1) : sizeCosts[_currentSize]);
			for (int j = 0; j < @int; j++)
			{
				_currentLongCost = (int)(1.2f * (float)_currentLongCost);
				_currentOilCost = (int)(1.2f * (float)_currentOilCost);
				_currentPowerCost = (int)(1.2f * (float)_currentPowerCost);
				_currentSizeCost = (int)(1.2f * (float)_currentSizeCost);
			}
			if (inventoryCount > 0)
			{
				bool flag7 = _currentLongCost > 0;
				bool flag8 = _currentOilCost > 0;
				bool flag9 = _currentPowerCost > 0;
				bool flag10 = _currentSizeCost > 0;
				for (int k = 0; k < inventoryCount; k++)
				{
					float num = 0.1f * (float)_currentLongCost;
					_currentLongCost = (int)((float)_currentLongCost - num);
					float num2 = 0.1f * (float)_currentOilCost;
					_currentOilCost = (int)((float)_currentOilCost - num2);
					float num3 = 0.1f * (float)_currentPowerCost;
					_currentPowerCost = (int)((float)_currentPowerCost - num3);
					float num4 = 0.1f * (float)_currentSizeCost;
					_currentSizeCost = (int)((float)_currentSizeCost - num4);
				}
				if (flag7)
				{
					_currentLongCost = Mathf.Max(1, _currentLongCost);
				}
				if (flag8)
				{
					_currentOilCost = Mathf.Max(1, _currentOilCost);
				}
				if (flag9)
				{
					_currentPowerCost = Mathf.Max(1, _currentPowerCost);
				}
				if (flag10)
				{
					_currentSizeCost = Mathf.Max(1, _currentSizeCost);
				}
			}
		}
		void SetIncrementalCost(int cost, GameObject rvButton, Button incrementalButton, Text RVText, TextMeshProUGUI text, bool isFreeOnboarding, GameObject onboardingArea)
		{
			if (isFreeOnboarding)
			{
				onboardingArea.SetActive(value: true);
				incrementalButton.interactable = true;
				rvButton.SetActive(value: false);
				text.text = Localization.GetTranslation("Generic_Free");
			}
			else
			{
				onboardingArea.SetActive(value: false);
				if (cost > 0)
				{
					float @float = NintendoPlayerPrefs.GetFloat("Money", 0f);
					incrementalButton.interactable = @float >= (float)cost;
					if (@float >= (float)cost)
					{
						text.color = white;
					}
					else
					{
						text.color = red;
					}
					text.text = cost + "$";
				}
				else
				{
					incrementalButton.interactable = false;
					rvButton.SetActive(value: false);
					text.text = Localization.GetTranslation("Generic_Max");
				}
			}
		}
	}

	public bool IsLengthLocked()
	{
		if (_currentLongCost < 0)
		{
			return true;
		}
		float @float = NintendoPlayerPrefs.GetFloat("Money", 0f);
		return (float)_currentLongCost > @float;
	}

	public bool IsPowerLocked()
	{
		if (_currentPowerCost < 0)
		{
			return true;
		}
		float @float = NintendoPlayerPrefs.GetFloat("Money", 0f);
		return (float)_currentPowerCost > @float;
	}

	public bool IsFuelLocked()
	{
		if (_currentOilCost < 0)
		{
			return true;
		}
		float @float = NintendoPlayerPrefs.GetFloat("Money", 0f);
		return (float)_currentOilCost > @float;
	}

	public bool IsSizeLocked()
	{
		if (_currentSizeCost < 0)
		{
			return true;
		}
		float @float = NintendoPlayerPrefs.GetFloat("Money", 0f);
		return (float)_currentSizeCost > @float;
	}

	private void ResizeButton(RectTransform button, bool small)
	{
		if (!small)
		{
			button.localScale = new Vector3(0.9f, 0.9f, 0.9f);
		}
		else
		{
			button.localScale = Vector3.one;
		}
	}

	private void ActiveRvButton(Button button, bool active)
	{
		Transform transform = button.transform;
		button.GetComponent<Mask>().showMaskGraphic = active;
		for (int i = 0; i < transform.childCount; i++)
		{
			transform.GetChild(i).GetComponent<Mask>().showMaskGraphic = active;
		}
		button.GetComponent<Image>().raycastTarget = active;
		button.interactable = active;
	}

	public void BuyLong(bool byRV = false)
	{
		if (byRV)
		{
			BuyLongByRV(success: true);
		}
		upgradeSource.Play();
		NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) - (float)_currentLongCost);
		int num = NintendoPlayerPrefs.GetInt("Long", 0) + 1;
		NintendoPlayerPrefs.SetInt("Long", num);
		SingletonMB<GameManager>.Instance.MoneySpent += _currentLongCost;
		_upgrade.UpdateLong();
		CheckButtons();
	}

	private void BuyLongByRV(bool success)
	{
		if (success)
		{
			NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) - (float)(_currentLongCost / 3));
			int num = NintendoPlayerPrefs.GetInt("Long", 0) + 1;
			NintendoPlayerPrefs.SetInt("Long", num);
			SingletonMB<SkinMissionController>.Instance.ProgressMission(SkinMissionType.UpgradeLength, 1);
			_upgrade.UpdateLong();
			CheckButtons();
		}
	}

	public void BuyOil(bool byRV = false)
	{
		if (byRV)
		{
			BuyOilByRV(success: true);
		}
		upgradeSource.Play();
		int num = NintendoPlayerPrefs.GetInt("Oil", 0) + 1;
		NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) - (float)_currentOilCost);
		NintendoPlayerPrefs.SetInt("Oil", num);
		SingletonMB<GameManager>.Instance.MoneySpent += _currentOilCost;
		_upgrade.UpdateOil();
		CheckButtons();
	}

	private void BuyOilByRV(bool success)
	{
		if (success)
		{
			int num = NintendoPlayerPrefs.GetInt("Oil", 0) + 1;
			NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) - (float)(_currentOilCost / 3));
			NintendoPlayerPrefs.SetInt("Oil", num);
			_upgrade.UpdateOil();
			CheckButtons();
		}
	}

	public void BuyPower(bool byRV = false)
	{
		if (byRV)
		{
			BuyPowerByRV(success: true, free: false);
		}
		upgradeSource.Play();
		int num = NintendoPlayerPrefs.GetInt("Power", 0) + 1;
		NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) - (float)_currentPowerCost);
		NintendoPlayerPrefs.SetInt("Power", num);
		SingletonMB<GameManager>.Instance.MoneySpent += _currentPowerCost;
		_upgrade.UpdatePower();
		CheckButtons();
	}

	private void BuyPowerByRV(bool success, bool free)
	{
		if (success)
		{
			if (!free)
			{
				NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) - (float)(_currentPowerCost / 3));
			}
			int num = NintendoPlayerPrefs.GetInt("Power", 0) + 1;
			NintendoPlayerPrefs.SetInt("Power", num);
			_upgrade.UpdatePower();
			CheckButtons();
		}
	}

	public void BuyPowerByForceRV()
	{
		BuyPowerByRV(success: true, free: true);
	}

	public void BuySize(bool byRV = false)
	{
		if (byRV)
		{
			BuySizeByRV(success: true, free: false);
		}
		upgradeSource.Play();
		int num = NintendoPlayerPrefs.GetInt("Size", 0) + 1;
		NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) - (float)_currentSizeCost);
		NintendoPlayerPrefs.SetInt("Size", num);
		SingletonMB<GameManager>.Instance.MoneySpent += _currentSizeCost;
		_upgrade.UpdateSize();
		CheckButtons();
	}

	private void BuySizeByRV(bool success, bool free)
	{
		if (success)
		{
			if (!free)
			{
				NintendoPlayerPrefs.SetFloat("Money", NintendoPlayerPrefs.GetFloat("Money", 0f) - (float)(_currentSizeCost / 3));
			}
			int num = NintendoPlayerPrefs.GetInt("Size", 0) + 1;
			NintendoPlayerPrefs.SetInt("Size", num);
			_upgrade.UpdateSize();
			CheckButtons();
		}
	}

	public void BuySizeByForceRV()
	{
		BuySizeByRV(success: true, free: true);
	}

	public List<UpgradeType> CanSomeUpgrade()
	{
		List<UpgradeType> list = new List<UpgradeType>();
		if (_longCost.text.Any(char.IsDigit))
		{
			list.Add(UpgradeType.Long);
		}
		if (_oilCost.text.Any(char.IsDigit))
		{
			list.Add(UpgradeType.Oil);
		}
		if (_powerCost.text.Any(char.IsDigit))
		{
			list.Add(UpgradeType.Power);
		}
		if (_sizeCost.text.Any(char.IsDigit))
		{
			list.Add(UpgradeType.Size);
		}
		return list;
	}

	public void FreeUpgrade(UpgradeType type)
	{
		if (type == UpgradeType.Long)
		{
			NintendoPlayerPrefs.SetInt("Long", NintendoPlayerPrefs.GetInt("Long", 0) + 1);
			SingletonMB<SkinMissionController>.Instance.ProgressMission(SkinMissionType.UpgradeLength, 1);
			_upgrade.UpdateLong();
		}
		if (type == UpgradeType.Oil)
		{
			NintendoPlayerPrefs.SetInt("Oil", NintendoPlayerPrefs.GetInt("Oil", 0) + 1);
			_upgrade.UpdateOil();
		}
		if (type == UpgradeType.Power)
		{
			NintendoPlayerPrefs.SetInt("Power", NintendoPlayerPrefs.GetInt("Power", 0) + 1);
			_upgrade.UpdatePower();
		}
		if (type == UpgradeType.Size)
		{
			NintendoPlayerPrefs.SetInt("Size", NintendoPlayerPrefs.GetInt("Size", 0) + 1);
			_upgrade.UpdateSize();
		}
		CheckButtons();
	}

	public void ResetAllSells()
	{
		NintendoPlayerPrefs.SetInt("Size", 0);
		NintendoPlayerPrefs.SetInt("Power", 0);
		NintendoPlayerPrefs.SetInt("Oil", 0);
		NintendoPlayerPrefs.SetInt("Long", 0);
	}
}
